﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LLClient.ClientServiceRef;
using LLClient.ServerProxy;

namespace LLClient.Portal
{
    /// <summary>
    /// Interaction logic for ChallangeControl.xaml
    /// </summary>
    public partial class ChallangeControl : UserControl
    {
        private Proxy _proxy = Proxy.GetInstance();
        private List<Client> availableUser;
        private bool _eventRegistered = false;
        private Client _currentClient = Proxy.GetInstance().CurrentClient;

        public Client CurrentClient
        {
            get { return _currentClient; }
            set { _currentClient = value; }
        }

        public ChallangeControl()
        {
            InitializeComponent();
            availableUser = new List<Client>();

            if (!_eventRegistered)
            {
                if (_proxy != null)
                    _proxy.ProxyCallbackEvent += _proxy_ProxyCallbackEvent;

                if (_proxy != null)
                    _proxy.ProxyEvent += _proxy_ProxyEvent;
                _eventRegistered = true;
            }
            _proxy.ProxyService.RefreshInitAvailableUser();
        }

        void _proxy_ProxyCallbackEvent(object sender, ProxyCallbackEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (e.Sender != null)
                {
                    switch (e.CallbackType)
                    {
                        case CallbackType.ReceiveChallange:
                            //MessageBox.Show(e.Sender.Username + " challanges you.", CurrentClient.Username);
                            string messageBoxText = e.Sender.Username + " challanges you.\nDo you accept it?";
                            string caption = CurrentClient.Username;
                            MessageBoxButton button = MessageBoxButton.YesNo;
                            
                            // Display message box
                            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, MessageBoxImage.Question);
                            if (result == MessageBoxResult.Yes)
                            {
                                _proxy.ProxyService.RespondChallange(e.Sender.Username, true);
                            }
                            else
                            {
                                _proxy.ProxyService.RespondChallange(e.Sender.Username, false);
                            }
                            break;
                        case CallbackType.ReceiveRespondChallange:
                            if (e.Message.Respond)
                            {
                                //MessageBox.Show(e.Sender.Username + " accepts your challange");
                            }
                            else
                            {
                                MessageBox.Show(e.Sender.Username + " refuses your challange");
                            }                            
                            break;
                        case CallbackType.ReceiveSendChallange:
                            if (e.Message.Respond)
                                MessageBox.Show("Your challange has been sent", CurrentClient.Username);
                            break;
                        case CallbackType.ClientLeave:
                            _proxy.ProxyService.Refresh();
                            break;
                    }
                }
            }));
        }

        void _proxy_ProxyEvent(object sender, ProxyEventArgs e)
        {
            if (!this.CheckAccess())
            {
                this.Dispatcher.BeginInvoke(new Action(() => this._proxy_ProxyEvent(sender, e)));
                return;
            }

            if (e.CallbackType == CallbackType.AvailableUserRefresh)
            {
                availableUser = e.Clients.ToList();
                availableListBox.Items.Clear();
                foreach (Client c in availableUser)
                {
                    if(c.Username != CurrentClient.Username)
                        availableListBox.Items.Add(c.Username);
                }
            }
        }

        private void challangeButton_Click(object sender, RoutedEventArgs e)
        {
            string target = availableListBox.SelectedItem.ToString();

            if (target != null && target != "")
            {
                _proxy.ProxyService.Challange(target);
            }
        }

        public void windowClose(object sender, EventArgs e)
        {
            if (_proxy != null)
                _proxy.ProxyCallbackEvent -= _proxy_ProxyCallbackEvent;
            _eventRegistered = false;
        }
    }
}
